
import { parseArgs } from 'node:util'
import { resolve,dirname } from 'node:path'
import { fileURLToPath } from 'node:url'
import esbuid from 'esbuild'  
import { createRequire } from 'node:module'
// 打包开发环境
// console.log('开始打包'); 
// console.log(process.argv)

const {values:{format},positionals} = parseArgs({
    allowPositionals:true,
    options:{
        format:{
            type:'string',
            short:'f',
            default:'esm'
        }
    }
})


// console.log(format,positionals)

// 创建 esm 的 __filename
const __filename = fileURLToPath(import.meta.url)
// 创建 esm 的 __dirname
const __dirname = dirname(__filename)

const require = createRequire(import.meta.url)

const target = positionals.length ? positionals[0]:'vue'

const entry = resolve(__dirname,`../packages/${target}/src/index.ts`)

// console.log('entry=>',entry)

const outfile = resolve(__dirname,`../packages/${target}/dist/${target}.${format}.js`)

const pkg = require(`../packages/${target}/package.json`)

console.log('pkg',pkg)

esbuid.context({
    entryPoints:[entry],
    outfile,
    format,
    platform: format === 'cjs' ? 'node': 'browser',
    sourcemap:true,
    bundle:true,
    globalName:pkg.buildOptions.name
}).then(ctx => ctx.watch())